/** 创建方块组合的类 */

import { deepCopy, rotateBlock } from "./BlockUtils";

export const ROWS = 4, COLS = 4;

/** 方块组合的情况 */
export const TETRIS_TYPES = [
  [
    [0, 1, 1, 0],
    [0, 0, 1, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 0]
  ],[
    [0, 1, 1, 0],
    [0, 1, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 0, 0]
  ],[
    [0, 1, 0, 0],
    [0, 1, 1, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 0]
  ],[
    [0, 0, 1, 0],
    [0, 1, 1, 0],
    [0, 1, 0, 0],
    [0, 0, 0, 0]
  ],[
    [0, 0, 0, 0],
    [0, 1, 1, 0],
    [0, 1, 1, 0],
    [0, 0, 0, 0]
  ],[
    [0, 0, 1, 0],
    [0, 1, 1, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 0]
  ],[
    [0, 0, 1, 0],
    [0, 0, 1, 0],
    [0, 0, 1, 0],
    [0, 0, 1, 0]
  ],
]


class Tetris {
  position = {
    row: 0,
    col: 0,
  };
  rotate = 0;
  blocks = null;
  constructor(type, rotate = 0, position = { row: 0, col: 0 }) {
    this.rotate = rotate;
    this.position = { ...position };
    this.blocks = deepCopy(TETRIS_TYPES[type], block => block * (type+1));
  }

  // 旋转
  rotate(rotate = -1) {
    rotateBlock(this.blocks, rotate);
  }

  // 移动
  move(row, col) {
    this.position.row += row;
    this.position.col += col;
  }
}

export default Tetris;